import datetime

import pymysql
import time
import random

connection = pymysql.connect(
    host='localhost',
    user='root',
    password='root',
    db='clouds'
)

cursor = connection.cursor()
print('正在生成数据...')
dev_id_list = [1, 2, 3, 4]
while True:

    dev_id = random.choice(dev_id_list)
    temp = round(random.uniform(0, 30), 2)
    hum = round(random.uniform(0, 100), 2)
    now = datetime.datetime.now()
    create_time = now.strftime("%Y-%m-%d %H:%M:%S")
    sql = f"INSERT INTO tb_temp_hum (dev_id, temp, hum, create_time) VALUES ({dev_id}, {temp}, {hum}, '{create_time}')"
    cursor.execute(sql)
    connection.commit()

    time.sleep(1)

cursor.close()
connection.close()